<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 系统JS加载组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Scripts extends Cola
{
	//系统JS
	protected static $scripts = array('');
	
    public static $template = '<script type="text/javascript" src="%s"></script>';

    public function set($string, $name = NULL, $after = NULL)
    {
		if(0 === strpos($string, 'http') || 0 === strpos($string, '/')){
			$string = sprintf(self::$template, $string);
		} else {
			$string = '<script type="text/javascript">'. $string . '</script>';
		}

		if(NULL !== $after && isset(self::$scripts[$after])){
			!is_string($name) ? self::$scripts[$after][] = $string : self::$scripts[$after][$name] = $string;
		} else {
			!is_string($name) ? self::$scripts[] = $string : self::$scripts[$name][$name] = $string;
		}
		return $this;
    }

    public function render()
    {
		$html = '';
        foreach (self::$scripts as $scripts) 
		{
			if (is_array($scripts)){
				foreach ($scripts as $script) $html .=  $script;
			} else
            $html .=  $scripts;
        }
		return $html;
    }
}
